﻿#include "stdafx.h"
#include "UISystem.h"
#include "GraphicScriptBinder.h"
#include "Widget.h"
#include "DebugAssert.h"

_NAMESPACE_BEGIN

UiSystem::UiSystem()
{

}

UiSystem::~UiSystem()
{
	
}

void UiSystem::Initial()
{

	mGUIRenderer = new CEGUI::OgreCEGUIRenderer(OgreProperty::Instance()->window,  
		Ogre::RENDER_QUEUE_OVERLAY, false, 3000, OgreProperty::Instance()->sceneManager);
	mGUISystem = new CEGUI::System(mGUIRenderer);   
	CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Errors);

	CEGUI::FontManager::getSingleton().createFont( "simhei-12.font" );
	CEGUI::FontManager::getSingleton().createFont( "bluehighway-12.font" );
	CEGUI::FontManager::getSingleton().createFont( "bluehighway-10.font" );
	CEGUI::FontManager::getSingleton().createFont( "bluehighway-8.font" );

	//载入皮肤
	CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"AquaLookSkin.scheme");
	CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
	//CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"WindowsLook.scheme");

	mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"AquaLook",(CEGUI::utf8*)"MouseArrow");
	CEGUI::MouseCursor::getSingleton().setImage("AquaLook","MouseArrow");
	mGUISystem->setDefaultFont((CEGUI::utf8*)"SimHei-12");
	mEditorGuiSheet=CEGUI::WindowManager::getSingleton().createWindow((CEGUI::utf8*)"DefaultWindow",
		(CEGUI::utf8*)"Sheet");  
	mGUISystem->setGUISheet(mEditorGuiSheet);
	
	SAVE_LOG("UI系统初始化完毕");
}	

void UiSystem::AddWindowFromRoot( CEGUI::Window* win )
{
	mEditorGuiSheet->addChildWindow(win);
}

void UiSystem::CreateImage( CONST_REF(string) resName, CONST_REF(string) name )
{
	CEGUI::Texture *cTex = UiSystem::Instance()->GetUIRender()->createTexture((CEGUI::utf8*)(resName.c_str()),"General");

	//UiSystem::Instance()->GetUIRender()
	string setName = name + "_ImageSet";
	CEGUI::Imageset *imageSet = CEGUI::ImagesetManager::getSingleton().createImageset((CEGUI::utf8*)(setName.c_str()), cTex);
	imageSet->defineImage((CEGUI::utf8*)(name.c_str()), 
		CEGUI::Point(0.0f, 0.0f),
		CEGUI::Size(cTex->getWidth(), cTex->getHeight()),
		CEGUI::Point(0.0f,0.0f));

	imageSets[name] = imageSet;
}

CEGUI::Imageset* UiSystem::GetImgSet( CONST_REF(string) name )
{
	return imageSets[name];
}

void UiSystem::RootAddWindow( WidgetWindow* window )
{
	mEditorGuiSheet->addChildWindow(window->GetWindow());
}

void UiSystem::AddComboBoxItem( CONST_REF(string) name,CONST_REF(string) item, int id )
{
	CEGUI::Combobox* objectComboBox = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	CEGUI::ListboxTextItem* itemText = new CEGUI::ListboxTextItem((CEGUI::utf8*)(item.c_str()), id);
	objectComboBox->addItem(itemText);
}

void UiSystem::AddListBoxItem( CONST_REF(string) name,CONST_REF(string) item,int id )
{
	CEGUI::Listbox* objectListbox = (CEGUI::Listbox*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	
	CEGUI::ListboxTextItem* itemText = new CEGUI::ListboxTextItem((CEGUI::utf8*)(item.c_str()), id);
	itemText->setSelectionBrushImage("TaharezLook", "ListboxSelectionBrush");
	objectListbox->addItem(itemText);
}

WidgetListBox* UiSystem::GetListBox( CONST_REF(string) name )
{
	CEGUI::Listbox* objectListbox = (CEGUI::Listbox*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	return new WidgetListBox(objectListbox);
}

WidgetComboBox* UiSystem::GetComboBox( CONST_REF(string) name )
{
	CEGUI::Combobox* objectComboBox = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	return new WidgetComboBox(objectComboBox);
}

WidgetButton* UiSystem::GetButton( CONST_REF(string) name )
{
	CEGUI::PushButton* objectButton = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	return new WidgetButton(objectButton);
}
WidgetRadioButton*	UiSystem::GetRadioButton( CONST_REF(string) name )
{
	CEGUI::RadioButton* objectRadioButton = (CEGUI::RadioButton*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	return new WidgetRadioButton(objectRadioButton);
}

WidgetEditBox*	UiSystem::GetEditBox( CONST_REF(string) name )
{
	CEGUI::Editbox* objectEditbox = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	return new WidgetEditBox(objectEditbox);
}

WidgetMultilineList* UiSystem::GetMultilineList( CONST_REF(string) name )
{
	CEGUI::MultiColumnList* objectMulColList = (CEGUI::MultiColumnList*)CEGUI::WindowManager::getSingleton().getWindow(name.c_str());
	return new WidgetMultilineList(objectMulColList);
}

void UiSystem::Release()
{
	mGUISystem->removeAllEvents();

	if(mEditorGuiSheet)
	{
		CEGUI::WindowManager::getSingleton().destroyAllWindows();
	}

	if(mGUISystem)
	{
		delete mGUISystem;
		mGUISystem = 0;
	}

	if(mGUIRenderer)
	{
		delete mGUIRenderer;
		mGUIRenderer = 0;
	}

	SAVE_LOG("CEGUI对象释放完毕");
}

_NAMESPACE_END

